<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://xiaoji.name All rights reserved.
// +----------------------------------------------------------------------
// | Author: jackie <297129853@qq.com>
// +----------------------------------------------------------------------
namespace plugins\aliyun_orc;

use cmf\lib\Plugin;
use think\Db;

class AliyunOrcPlugin extends Plugin
{
    public $info = array(
        'name'=>'AliyunOrc',
        'title'=>'阿里云身份证OCR文字识别',
        'description'=>'阿里云身份证OCR文字识别',
        'status'=>1,
        'author'=>'',
        'version'=>'1.0'
    );
    
    public $has_admin=0;//插件是否有后台管理界面
    
    public function install()
    {//安装方法必须实现
    	$where_data1 = [
            'type' => 1,
            'once' => 1,
            'name' => '阿里云身份证OCR',
            'hook' => 'get_ocr_idcard',
            'app' => 'cmf',
            'description' => '阿里云身份证OCR'
        ];
        $test = Db::name('hook')->where($where_data1)->find();
        if (empty($test)) {
            Db::name('hook')->insert($where_data1);
        }
        return true;//安装成功返回true，失败false
    }
    
    public function uninstall()
    {//卸载方法必须实现
    	$where_data1 = [
            'type' => 1,
            'once' => 1,
            'name' => '阿里云身份证OCR',
            'hook' => 'get_ocr_idcard',
            'app' => 'cmf',
            'description' => '阿里云身份证OCR'
        ];
        $test = Db::name('hook')->where($where_data1)->find();
        if (!empty($test)) {
            Db::name('hook')->where($where_data1)->delete();
        }
        return true;//卸载成功返回true，失败false
    }

    //$result = hook_one("get_ocr_idcard", $param);
    public function getOcrIdcard($param)
    {
    	$config        = $this->getConfig();
    	$url = "https://cardnumber.market.alicloudapi.com/rest/160601/ocr/ocr_idcard.json";
	    $appcode = $config['appCode'];
	    $method = "POST";

	    $headers = array();
	    array_push($headers, "Authorization:APPCODE " . $appcode);
	    //根据API的要求，定义相对应的Content-Type
	    array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");

	    //如果没有configure字段，config设为空
	    $config = array(
	        "side"=>"face"
	    );

	    $img_data=$this->img_base64($param['img_path']);
	    $request = array(
	        "image" => "$img_data"
	    );
	    if(count($config) > 0){
	        $request["configure"] = json_encode($config);
	    }
	    $body = json_encode($request);

	    $curl = curl_init();
	    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
	    curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	    curl_setopt($curl, CURLOPT_FAILONERROR, false);
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($curl, CURLOPT_HEADER, true);
	    if (1 == strpos("$".$url, "https://")){
	        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	    }
	    curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
	    $result = curl_exec($curl);
	    $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
	    $rheader = substr($result, 0, $header_size);
	    $rbody = substr($result, $header_size);

	    $httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
	    if($httpCode == 200){
	        $result = array(
				'code'=>1,
				'msg'=>'请求成功！',
				'data'=>json_decode($rbody)
			);
	    }else{
	        $result = array(
				'code'=>0,
				'msg'=>'请求失败！',
				'data'=>json_decode($rbody)
			);
	    }
	    return $result;
    }

    function img_base64($path){
        //对path进行判断，如果是本地文件就二进制读取并base64编码，如果是url,则返回
        $img_data="";
        if (substr($path,0,strlen("http")) === "http"){
            $img_data = $path;
        }else{
            if($fp = fopen($path, "rb", 0)) {
                $binary = fread($fp, filesize($path)); // 文件读取
                fclose($fp);
                $img_data = base64_encode($binary); // 转码
            }else{
                printf("%s 图片不存在",$img_path);
            }
        }
        return $img_data;
    }

}
